package shouc;

import java.sql.SQLException;
import java.util.List;

import jdbc.JDBCUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import mybeans.Beans;
import mybeans.Page;

public class MyShouService {

	public Page getPage(String userID) {
		QueryRunner qr = new QueryRunner(JDBCUtils.getDs());
		String sql = "select count(*) as sum from Shou where userID = ?";
		try {
			return qr.query(sql, userID, new BeanHandler<>(Page.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public List<Beans> getAPageShou(Page pageBean, String userID) {
		
		int c=getPage(userID).getSum();
		int countpage1=c-pageBean.getNuminpage()*(pageBean.getCurrentpage()-1);
		String count=Integer.toString(countpage1);
		
		
		QueryRunner qr = new QueryRunner(JDBCUtils.getDs());
		String sql = "select top "+pageBean.getNuminpage()+"* from (select top "+count+"path, PlantPic.plantID  from Shou,PlantPic where Shou.plantID = PlantPic.plantID and userID = ? order by PlantPic.plantID desc)as g order by g.plantID asc";
		
		
		try {
			return qr.query(sql, userID, new BeanListHandler<>(Beans.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

}
